package com.wmh.baseservice.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.merchant.pojo.MerchantGoods;
import com.wmh.baseservice.merchant.mapper.MerchantGoodsMapper;
import com.wmh.baseservice.skd.pojo.SkdGoodsSpec;
import com.wmh.baseservice.merchant.service.MerchantGoodsService;
import com.wmh.baseservice.common.mybatisplus.methods.CommonServiceImpl;
import com.wmh.baseservice.skd.service.SkdGoodsSpecService;
import com.wmh.baseservice.user.pojo.Goods;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author wmh generator
 * @since 2022-01-19
 */
@Service
public class MerchantGoodsServiceImpl extends CommonServiceImpl<MerchantGoodsMapper, MerchantGoods> implements MerchantGoodsService {

    @Resource
    private MerchantGoodsMapper merchantGoodsMapper;
    @Resource
    private SkdGoodsSpecService merchantSpecService;

    @Override
    public IPage<Goods> goodList(Page<Goods> vegetablePage, Map<String, Object> map) {
        return merchantGoodsMapper.goodList(vegetablePage, map);
    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void add(MerchantGoods merchantGoods, List<SkdGoodsSpec> skdGoodsSpecs) {
//        if (!save(merchantGoods)) {
//            throw new ServiceException();
//        }
//        if (skdGoodsSpecs.size() != 0 && merchantGoods.getHasSpec() == 1) {
//            skdGoodsSpecs.forEach(item -> item.setGoodsId(merchantGoods.getId()).setType(1));
//            if (!merchantSpecService.saveBatch(skdGoodsSpecs)) {
//                throw new ServiceException();
//            }
//        }
//
//    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void updateGoods(MerchantGoods merchantGoods, List<SkdGoodsSpec> skdGoodsSpecs) {
//        if (!updateById(merchantGoods)) {
//            throw new ServiceException();
//        }
//        if (skdGoodsSpecs.size() != 0) {
//            //删除原规格
//            if (!merchantSpecService.remove(new QueryWrapper<SkdGoodsSpec>().eq(SkdGoodsSpec.TYPE, 1).eq(SkdGoodsSpec.GOODS_ID, merchantGoods.getId()))) {
//                throw new ServiceException();
//            }
//            skdGoodsSpecs.forEach(item -> item.setGoodsId(merchantGoods.getId()).setType(1));
//            //添加规格
//            if (!merchantSpecService.saveBatch(skdGoodsSpecs)) {
//                throw new ServiceException();
//            }
//        }
//
//    }
}
